import PlayerData from "../data/PlayerData";
import Game from "../Game";
import { ui } from "../../../ui/layaMaxUI";

import PlayerScript from "../../component/PlayerScript";
import HeroBullet from "./HeroBullet";
import HeroVO from "../../db/vo/HeroVO";
import GlobalData from "../../db/GlobalData";



export default class Hero {


    _sp3d: Laya.Sprite3D;

    public playerData: PlayerData = new PlayerData();

    public bullet: HeroBullet = new HeroBullet();

    _comp: PlayerScript;

    private _startRotation: number;

    _heroVO: HeroVO;


    public get sp3d() {
        return this._sp3d;
    }

    init(): void {
        let attackSpeedHeroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, this.playerData.attackSpeedLv);
        this.playerData.attackSpeedValue = attackSpeedHeroVO.attackSpeedValue;
        let attackHeroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, this.playerData.attackLv);
        this.playerData.attack = attackHeroVO.attack;
        // console.log("Hero init.",this.playerData);
        let shadow = new ui.moduleView.common.ShadowViewUI();

        Game.footLayer.addChild(shadow);

        this._sp3d = Laya.loader.getRes(this.playerData.heroResPath) as Laya.Sprite3D;

        Game.layer3d.addChild(this._sp3d);
        this._sp3d.transform.position = new Laya.Vector3(0, 0, -0.1);
        let outPutUIPos: Laya.Vector4 = new Laya.Vector4();
        Game.camera.viewport.project(this._sp3d.transform.position, Game.projectViewMat, outPutUIPos);
        shadow.pos(outPutUIPos.x + 5, outPutUIPos.y + 28);
        shadow.scaleX = 2.6;
        shadow.scaleY = 2.5;
        if (this._comp == null) {
            this._comp = this._sp3d.addComponent(PlayerScript);
        }
        if (this._startRotation == null) {
            this._startRotation = (this._sp3d.getChildByName("weapon") as Laya.Sprite3D).transform.localRotationEulerY;

        }

        (this._sp3d.getChildByName("weapon") as Laya.Sprite3D).transform.localRotationEulerY = this._startRotation;
    }

    resetFireTime() {
        this._comp.preFireTime = Laya.Browser.now() + 300;
    }

    initBullet() {
        this.bullet.init(this._comp.weapon);
    }

    updateState() {
        let attackSpeedHeroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, this.playerData.attackSpeedLv);
        this.playerData.attackSpeedValue = attackSpeedHeroVO.attackSpeedValue;
        let attackHeroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, this.playerData.attackLv);
        this.playerData.attack = attackHeroVO.attack;
        this.bullet.updateState();
    }






}